#include <sstream>
#include "miniutil/simpledataware.h"

namespace miniutil { namespace frame {

    SimpleDataWareValue::SimpleDataWareValue()
    {
        vtype = VALUE_TYPE_UNKNOWN;
        bvle = false;
        ivle = 0;
    }

    SimpleDataWareValue::SimpleDataWareValue(bool bv)
    {
        SetValue(bv);
    }
            
    SimpleDataWareValue::SimpleDataWareValue(int iv)
    {
        SetValue(iv);
    }
    
    SimpleDataWareValue::SimpleDataWareValue(const std::string& sv)
    {
        SetValue(sv);
    }

    SimpleDataWareValue::SimpleDataWareValue(const char* pvle)
    {
        std::string sv;
        if(pvle != NULL)
            sv = pvle;
        SetValue(sv);
    }

    int SimpleDataWareValue::SetValue(const std::string& sv)
    {
        vtype = VALUE_TYPE_STR;
        svle = sv;
        if(svle.empty() || svle == "false")
        {
            bvle = false;
            ivle = 0;
        }
        else
        {
            bvle = true;
            ivle = 1;
        }
        return 0;
    }

    int SimpleDataWareValue::SetValue(int iv)
    {
        vtype = VALUE_TYPE_INT;
        ivle = iv;
        if(ivle != 0)
            bvle = true;
        else
            bvle = false;
        std::stringstream ss;
        ss<<iv;
        svle = ss.str();
        return 0;
    }

    int SimpleDataWareValue::SetValue(bool bv)
    {
        vtype = VALUE_TYPE_BOOL;
        bvle = bv;
        if(bvle)
        {
            ivle = 1;
            svle = "true";
        }
        else
        {
            ivle = 0;
            svle = "false";
        }
        return 0;
    }

    //-------------------------------------------------------
    SimpleDataWare* SimpleDataWare::pinst = NULL;
    SimpleDataWare* SimpleDataWare::GetInstance()
    {
        if(pinst == NULL)
            pinst = new SimpleDataWare();
        return pinst;
    }        
    
    void SimpleDataWare::DeleteInstance()
    {
        if(pinst != NULL)
        {
            delete pinst;
            pinst = NULL;
        }
    }

    SimpleDataWare::SimpleDataWare()
    {
    }

    SimpleDataWare::~SimpleDataWare()
    {
    }


    int SimpleDataWare::SetValue(std::string key, bool vle)
    {
        ClearKey(key);
        mmallvles.insert(std::make_pair(key, SimpleDataWareValue(vle)));
        return 0;
    }

    int SimpleDataWare::SetValue(std::string key, int vle)
    {
        ClearKey(key);
        mmallvles.insert(std::make_pair(key, SimpleDataWareValue(vle)));
        return 0;
    }

    int SimpleDataWare::SetValue(std::string key, const std::string & vle)
    {
        ClearKey(key);
        mmallvles.insert(std::make_pair(key, SimpleDataWareValue(vle)));
        return 0;
    }

    int SimpleDataWare::SetValue(std::string key, const char* pv)
    {
        std::string sv;
        if(pv != NULL)
            sv = pv;
        SetValue(key, sv);
        return 0;
    }
    //
    bool SimpleDataWare::IsKeyAvailable(std::string key)
    {
        if(mmallvles.find(key) == mmallvles.end())
            return false;
        return true;
    }
        
    bool SimpleDataWare::GetBoolValue(std::string key)
    {
        if(mmallvles.find(key) == mmallvles.end())
            return false;
        
        return mmallvles[key].bvle;
    }

    int SimpleDataWare::GetIntValue(std::string key)
    {
        if(mmallvles.find(key) == mmallvles.end())
            return 0;
        
        return mmallvles[key].ivle;
    }

    std::string SimpleDataWare::GetStrValue(std::string key)
    {
        if(mmallvles.find(key) == mmallvles.end())
            return std::string("");
        
        return mmallvles[key].svle;
    }
    
    int SimpleDataWare::ClearKey(std::string key)
    {
        if(mmallvles.find(key) != mmallvles.end())
        {            
            mmallvles.erase(key);
            return 0;
        }
        
        std::string subkey = key+".";
        std::map<std::string, SimpleDataWareValue>::iterator iter_msc;
        for(iter_msc = mmallvles.begin(); iter_msc != mmallvles.end(); )
        {
            if(iter_msc->first.find(subkey) != std::string::npos)
            {
                mmallvles.erase(iter_msc++);
                continue;
            }                
            iter_msc ++;
        }            
        return 0;
    }
    
} }

    